package liuliu.throughwaring.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import liuliu.custom.control.dialog.LLDialog;
import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.adapter.CommonAdapter;
import liuliu.throughwaring.adapter.ViewHolder;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.control.add.police.company.IRegUnitView;
import liuliu.throughwaring.control.task.addTask;
import liuliu.throughwaring.model.CodeModel;
import liuliu.throughwaring.model.CompanyModel;
import liuliu.throughwaring.model.TerminalModel;
import liuliu.throughwaring.ui.map.MultipleRoutePlanningActivity;

/**
 * Created by Administrator on 2016/2/26.
 */
public class NewCompanyList extends BaseActivity implements IRegUnitView {
    @CodeNote(id = R.id.new_company_toolbar)
    TToolbar title;
    @CodeNote(id = R.id.new_company_list)
    ListView lv;
    List<CompanyModel> list;
    List<CodeModel> mCodeList;
    LLDialog llDialog;//提示是否接收企业信息
    addTask add;
    CompanyModel cm;
    @CodeNote(id = R.id.btn_weijieshou)
    ImageButton btnWei;
    @CodeNote(id = R.id.btn_yijieshou)
    ImageButton btnYi;
    String pid = "";
    @CodeNote(id = R.id.new_company_mes)
    TextView mes;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_new_company_list);
        add = new addTask(this);
        mUtils = new Utils(this);
        // List<TerminalModel> terminalList=finalDb.findAllByWhere(TerminalModel.class,"TerminalCode='"+Utils.getImei(this)+"'");
        mCodeList = finalDb.findAllByWhere(CodeModel.class, "codeName='Code_CompanyType'");
        list = null;
        pid = Utils.ReadString("PoliceId");

        //  if (getIntent().getStringExtra("isRecive").equals("")) {
        //已经接收的
        //list = finalDb.findAllByWhere(CompanyModel.class, "CompanyPoliceId='" + pid + "' and IsPush ='1'");
        // } else {
        //未接收
        list = finalDb.findAllByWhere(CompanyModel.class, "IsPush='null' or IsPush='False'");//推送
        // }

        btnWei.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list = null;
                list = finalDb.findAllByWhere(CompanyModel.class, "IsPush='null' or IsPush='False' order by CompanyCreateTime");//推送
                initEvents();
                btnWei.setBackgroundResource(R.mipmap.wei_bai);
                btnYi.setBackgroundResource(R.mipmap.yi_lan);
            }
        });
        btnYi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list = null;
                if (!pid.equals("")) {
                    list = finalDb.findAllByWhere(CompanyModel.class, "CompanyPoliceId='" + pid + "' and IsPush ='True' order by CompanyChangeTime");
                    initEvents();
                } else {
                    list = null;
                }
                btnWei.setBackgroundResource(R.mipmap.wei_lan);
                btnYi.setBackgroundResource(R.mipmap.yi_bai);

            }
        });
        title.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                finish();
            }
        });
    }

    @Override
    public void initEvents() {
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();

                intent.setClass(NewCompanyList.this, UnitDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("CompanyModel", (Serializable) list.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        if (null != list && list.size() != 0) {
            lv.setAdapter(new CommonAdapter<CompanyModel>(this, list,
                    R.layout.item_company_tuisong) {
                @Override
                public void convert(final ViewHolder holder, final CompanyModel model, int position) {
                    if (model.getCompanyName().length() > 14) {
                        holder.setText(R.id.unit_search_item_main_unit_name, model.getCompanyName().substring(0, 14) + "...");
                    } else {
                        holder.setText(R.id.unit_search_item_main_unit_name, model.getCompanyName());
                    }
                    holder.setOnClickListener(R.id.unit_search_tel, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + model.getCompanyPhoneNumber()));
                            startActivity(intent);
                        }
                    });
                    String str = (getCodeyCompanyValue(model.getCompanyType())).substring(0, 2);
                    holder.setText(R.id.unit_search_item_main_unit_state, str);
                    holder.setText(R.id.unit_search_item_main_tel, model.getCompanyPhoneNumber());
                    // holder.setText(R.id.unit_search_item_main_create_time, model.getCompanyCreateTime());
                    holder.setText(R.id.unit_search_item_main_address, model.getCompanyAddress());
                    holder.setOnClickListener(R.id.tuisong_daohang, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(NewCompanyList.this, MultipleRoutePlanningActivity.class);
                            startActivity(intent);
                        }
                    });
                    holder.setOnClickListener(R.id.tuisong_recive, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            llDialog = new LLDialog(NewCompanyList.this);
                            llDialog.setMiddleMessage("确认要接收企业信息？");
                            llDialog.setOnPositiveListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    // finish();
                                    llDialog.dismiss();
                                    //改变状态，修改企业信息
                                    cm = model;
                                    cm.setIsPush("True");
                                    add.addCompany(cm, NewCompanyList.this);
                                }
                            });
                            llDialog.show();
                        }
                    });
                    if (model.getIsPush().equals("True")) {
                        holder.setVisible(R.id.tuisong_recive, false);
                    } else {
                        holder.setVisible(R.id.tuisong_recive, true);
                    }
                }
            });
            mes.setVisibility(View.GONE);
            lv.setVisibility(View.VISIBLE);
        } else {
            mes.setVisibility(View.VISIBLE);
            lv.setVisibility(View.GONE);
        }
    }

    private String getCodeyCompanyValue(String value) {
        for (int i = 0; i < mCodeList.size(); i++) {
            if (value.equals(mCodeList.get(i).getName())) {
                return mCodeList.get(i).getValue();//流动人员
            }
        }
        return "";
    }

    @Override
    public void onRegUnitResult(boolean isTrue, String mes) {
        if (isTrue) {
            mUtils.ToastShort("接收成功");
//            Intent intent = new Intent(NewCompanyList.this, AddWorkActivity.class);
//            Bundle bundle = new Bundle();
//            bundle.putSerializable("CompanyModel", (Serializable) cm);
//            intent.putExtras(bundle);
//            startActivity(intent);
            list = null;
            list = finalDb.findAllByWhere(CompanyModel.class, "IsPush='null' or IsPush='False'");//推送
            initEvents();
        } else {
            mUtils.ToastShort("接收失败");
        }
    }
}
